﻿using BI.CF.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BI.CF.BLL;
using BI.CF.UI.Models;
using System.Text;
using BI.CF.Common;

namespace BI.CF.Company.Controllers
{
    public class SecurityCenterController : Controller
    {
        UserBLL Ubll = new UserBLL();
        /// <summary>
        /// 用户修改密码界面
        /// </summary>
        /// <returns></returns>
        public ActionResult UserPwdUpd()
        {
            return View();
        }
        /// <summary>
        /// 返回当前登录人信息
        /// </summary>
        /// <returns></returns>
        public JsonResult UserPwdTest()
        {
            UserrsModel Umodel = Session["Umodel"] as UserrsModel;
            return Json(Umodel);
        }
        /// <summary>
        /// 用户修改密码
        /// </summary>
        /// <returns></returns>
        public JsonResult UserpPwdUpd()
        {
            int UserId = Convert.ToInt32(Request["UserId"]);
            string NewPwd = Request["NewPwd"].ToString();
            if (Ubll.UserBinDingPhone(UserId, NewPwd) != 0)
            {
                return Json("1");
            }
            else
            {
                return Json("0");
            }
        }
        /// <summary>
        /// 用户绑定手机页面
        /// </summary>
        /// <returns></returns>
        public ActionResult UserRegisterPhone()
        {
            return View();
        }
        /// <summary>
        /// 发送验证码
        /// </summary>
        /// <returns></returns>
        public JsonResult GetPhonePwd()
        {
           
            YunPianSmsClient YunPian = new YunPianSmsClient();
            if (Request["Phone"] != null)
            {
                YunPian.SendSms(Request["Phone"].ToString());
                int i = Convert.ToInt32(Session["PhoneCodeString"]);
                return Json(i);
            }
            else
            {
                return Json(0);
            }
        }
        /// <summary>
        /// 用户绑定手机方法
        /// </summary>
        /// <returns></returns>
        public JsonResult BinDingUserPhone()
        {
             UserrsModel Umodel = Session["Umodel"] as UserrsModel;
             if (Ubll.UserBinDingPhone(Umodel.UserId, Request["Phone"].ToString()) != 0)
             {
                 return Json("1");
             }
             else
             {
                 return Json("0");
             }
        }
        /// <summary>
        /// 用户绑定
        /// </summary>
        /// <returns></returns>
        public ActionResult UserBinDingEmail()
        {
            return View();
        }
        /// <summary>
        /// 发送邮件验证码 
        /// </summary>
        /// <returns></returns>
        public int GetEmailActivationCode()
        {
            UserrsModel Umodel=Session["Umodel"] as UserrsModel;
            string mailTo=Request["Email"].ToString();
            string mailSubject="诚信金融邮箱激活码";
            StringBuilder mailContent = new StringBuilder();
            mailContent.Append("尊敬的用户:");
            mailContent.Append(Umodel.UserName);
            mailContent.Append("您好，您的激活码是:");
            string code = GetPhoneCode();
            mailContent.Append(code);
            mailContent.Append("，请将激活码进行验证");
            if (MailHelper.SendEmail(mailTo, mailSubject, mailContent.ToString()))
            {
                return Convert.ToInt32(code);
            }
            else
            {
                return 0;
            }
        }
        /// <summary>
        /// 用户绑定邮箱
        /// </summary>
        /// <returns></returns>
        public int SendUserEmail()
        {
            UserrsModel Umodel = Session["Umodel"] as UserrsModel;
            string Email = Request["Email"].ToString();
            if (Ubll.UserBinDingBankCard(Umodel.UserId, Email) != 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        /// <summary>
        /// 获取手机验证码
        /// </summary>
        /// <returns></returns>
        public string GetPhoneCode()
        {
            Random objDom = new Random();
            StringBuilder BuilderString = new StringBuilder();
            for (int i = 0; i < 10; i++)
            {
                BuilderString.Append(i);
            }
            string PhoneCodeString = "";
            for (int i = 0; i < 6; i++)
            {
                int index = objDom.Next(0, BuilderString.Length);
                PhoneCodeString += index;
                BuilderString.Remove(index, 1);
            }
            Session["PhoneCodeString"] = PhoneCodeString;
            return PhoneCodeString;

        }
        /// <summary>
        /// 用户绑定银行卡
        /// </summary>
        /// <returns></returns>
        public ActionResult UserBindingBackCard()
        {
            return View();
        }
        /// <summary>
        /// 获取开户行名称
        /// </summary>
        /// <returns></returns>
        public JsonResult GetBankName()
        {
            string BankCardId = Request["BankCardId"].ToString();
            BankCardId=BankCardId.Replace(" ", ""); 
            char[] chars = BankCardId.ToCharArray();
            return Json(BankInfo.GetBankName(chars));
        }
        /// <summary>
        /// 用户绑定银行卡
        /// </summary>
        /// <returns></returns>
        public ActionResult BindingBankid()
        {
            string Bankno = Request["BankCard"].ToString();
            UserrsModel Umodel = Session["Umodel"] as UserrsModel;
            if (Ubll.UserBinDingBankCard(Umodel.UserId, Bankno) != 0)
            {
                return Content("<script>alert('绑定成功');location.href='/SecurityCenter/MyaccountSecurityCenter'</script>");
            }
            else
            {
                return Content("<script>alert('绑定错误');location.href='/SecurityCenter/MyaccountSecurityCenter'</script>");
            }
           
        }
    }
}
